<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var user = {
        name: "hyt",
        age: 18,
        gender: "male",
      };

      Object.prototype.city = "New York";

      function detectProperty(obj, propName) {
        if (obj.hasOwnProperty(propName)) {
          console.log("该属性是自有属性");
        } else if (propName in obj) {
          console.log("该属性是继承属性");
        } else {
          console.log("对象中不存在 " + propName + " 属性");
        }
      }

      detectProperty(user, "name"); // 该属性是自有属性
      detectProperty(user, "city"); // 该属性是继承属性
      detectProperty(user, "666"); // 对象中不存在 666 属性
    </script>
  </body>
</html>
